package com.spic.business.hidden.type.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import com.spic.business.hidden.type.domain.vo.HiddenTypeTree;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spic.business.hidden.type.mapper.HiddenTypeMapper;
import com.spic.business.hidden.type.domain.HiddenType;
import com.spic.business.hidden.type.service.IHiddenTypeService;

/**
 * 隐患类型Service业务层处理
 *
 * @author qishaoping
 * @date 2021-10-17
 */
@Service
public class HiddenTypeServiceImpl implements IHiddenTypeService
{
    @Autowired
    private HiddenTypeMapper hiddenTypeMapper;

    /**
     * 查询隐患类型
     *
     * @param id 隐患类型主键
     * @return 隐患类型
     */
    @Override
    public HiddenType selectHiddenTypeById(String id)
    {
        return hiddenTypeMapper.selectHiddenTypeById(id);
    }

    /**
     * 查询隐患类型列表
     *
     * @param hiddenType 隐患类型
     * @return 隐患类型
     */
    @Override
    public List<HiddenType> selectHiddenTypeList(HiddenType hiddenType)
    {
        return hiddenTypeMapper.selectHiddenTypeList(hiddenType);
    }

    /**
     * 新增隐患类型
     *
     * @param hiddenType 隐患类型
     * @return 结果
     */
    @Override
    public int insertHiddenType(HiddenType hiddenType)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        hiddenType.setId(uuid);
        return hiddenTypeMapper.insertHiddenType(hiddenType);
    }

    /**
     * 修改隐患类型
     *
     * @param hiddenType 隐患类型
     * @return 结果
     */
    @Override
    public int updateHiddenType(HiddenType hiddenType)
    {
        return hiddenTypeMapper.updateHiddenType(hiddenType);
    }

    /**
     * 批量删除隐患类型
     *
     * @param ids 需要删除的隐患类型主键
     * @return 结果
     */
    @Override
    public int deleteHiddenTypeByIds(String[] ids)
    {
        return hiddenTypeMapper.deleteHiddenTypeByIds(ids);
    }

    /**
     * 删除隐患类型信息
     *
     * @param id 隐患类型主键
     * @return 结果
     */
    @Override
    public int deleteHiddenTypeById(String id)
    {
        return hiddenTypeMapper.deleteHiddenTypeById(id);
    }

    @Override
    public List<HiddenType> hiddenTypeSelect(HiddenType hiddenType) {
        hiddenType.setParentId("0");
        return hiddenTypeMapper.hiddenTypeSelect(hiddenType);
    }

    @Override
    public List<HiddenType> seconddaryHidden(String parentId) {
        return hiddenTypeMapper.seconddaryHidden(parentId);
    }

    @Override
    public List<HiddenTypeTree> buildTreeSelect(List<HiddenType> hiddenTypeList){
        List<HiddenType> deptTrees = buildPlaceTree(hiddenTypeList);
        return deptTrees.stream().map(HiddenTypeTree::new).collect(Collectors.toList());
    }


    private List<HiddenType> buildPlaceTree(List<HiddenType> hiddenTypes)
    {
        List<HiddenType> returnList = new ArrayList<HiddenType>();
        List<String> tempList = new ArrayList<String>();
        for (HiddenType hiddenType : hiddenTypes)
        {
            tempList.add(hiddenType.getId());
        }
        for (Iterator<HiddenType> iterator = hiddenTypes.iterator(); iterator.hasNext();)
        {
            HiddenType hiddenType = (HiddenType) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(hiddenType.getParentId()))
            {
                recursionFn(hiddenTypes, hiddenType);
                returnList.add(hiddenType);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = hiddenTypes;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<HiddenType> list, HiddenType t)
    {
        // 得到子节点列表
        List<HiddenType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (HiddenType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<HiddenType> getChildList(List<HiddenType> list, HiddenType t)
    {
        List<HiddenType> tlist = new ArrayList<HiddenType>();
        Iterator<HiddenType> it = list.iterator();
        while (it.hasNext())
        {
            HiddenType n = (HiddenType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<HiddenType> list, HiddenType t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }


}
